using System.Collections;
using System.Collections.Generic;
using Spine.Unity;
using UnityEngine;
using UnityEngine.Serialization;

public class SkeletonUtilityDragConstraint : SkeletonUtilityConstraint
{
    public Transform target;

    public SkeletonGraphic skeletonGraphic;

    public Vector2 Offset;

    public float Weight;

    public override void DoUpdate()
    {
        if (bone == null)
        {
            return;
        }

        if (bone.bone == null)
        {
            return;
        }

        if (hierarchy == null)
        {
            return;
        }
        
        var oriPos = new Vector2();
        oriPos.x = bone.bone.X * hierarchy.PositionScale;
        oriPos.y = bone.bone.Y * hierarchy.PositionScale;
        transform.localPosition = oriPos;
        oriPos = transform.position;
        
        Vector2 ctlPos = new Vector2(target.transform.position.x,target.transform.position.y) + Offset;
        Vector2 finalPos = Vector2.Lerp(oriPos, ctlPos, Weight);
        
        transform.position = finalPos;
        
        bone.bone.X = transform.localPosition.x / hierarchy.PositionScale;
        bone.bone.Y = transform.localPosition.y / hierarchy.PositionScale;
    }
}
